package com.zhongying.jf.util;

import com.zhongying.jf.ResConstants;
import com.zhongying.jf.exception.ZhongyingiException;
import com.zhongying.jf.util.vaild.ValidatorAssert;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @description 用户工具类
 * @auther zengli
 * @create 2017/9/27
 */
public class UserUtil {

    /**
     * 获取userFlag
     * @param client
     * @param userCode
     * @return
     */
    public static String getUserFlag(String client, String userCode) {
        String userflag = "";
        if ("1".equals(client)) {
            userflag = "p2p.cache.dev" + userCode + "PC";
        } else if ("2".equals(client)) {
            userflag = "p2p.cache.dev" + userCode + "WC";
        } else if ("3".equals(client)) {
            userflag = "p2p.cache.dev" + userCode + "IOS";
        } else if ("4".equals(client)) {
            userflag = "p2p.cache.dev" + userCode + "AD";
        }
        return userflag;
    }

    /**
     * 获取userFlag
     */
    public static String getUserFlag(Map inMap){
        //客户端
        String Client="";
        if(inMap.get("Client")!=null){
            Client=inMap.get("Client").toString();
        }
        if(inMap.get("client")!=null){
            Client=inMap.get("client").toString();
        }
        //用户编号
        String UserCode="";
        if(inMap.get("UserCode")!=null){
            UserCode=inMap.get("UserCode").toString();
        }
        if(inMap.get("userCode")!=null){
            UserCode=inMap.get("userCode").toString();
        }
        String userFlag="";
        if ("1".equals(Client)) {
            userFlag = "p2p.cache.dev" + UserCode + "PC";
        } else if ("2".equals(Client)) {
            userFlag = "p2p.cache.dev" + UserCode + "WC";
        } else if ("3".equals(Client)) {
            userFlag = "p2p.cache.dev" + UserCode + "IOS";
        } else if ("4".equals(Client)) {
            userFlag = "p2p.cache.dev" + UserCode + "AD";
        }
        return userFlag;
    }

    /**
     * 主要验证用户四个信息:用户状态 开户 绑卡 交易密码
     * @param userStatusMap
     */
    public static void validatorUserInfo(Map<String, Object> userStatusMap) throws ZhongyingiException {
        MapUtil mapUtil = new MapUtil(userStatusMap);
        ValidatorAssert.isTrue(mapUtil.getString("userStatus").equals("1"),"用户状态不正常");
        ValidatorAssert.isTrue(mapUtil.getString("openAccountStatus").equals("3"),"用户未开户");
        ValidatorAssert.isTrue(mapUtil.getString("bindBankCardFlag").equals("1"),"用户未绑定银行卡");
        ValidatorAssert.isTrue(mapUtil.getString("setPasswordFlag").equals("1"),"用户未设置交易密码");
    }

    /**
     * 计算预期收益 不包含本金
     *
     * @param amount       金额
     * @param rate         年化利率
     * @param periodLength 投资周期长度
     * @param periodUnit   投资周期单位 1:天 2:周 3:月 4:年
     * @param profitPlan   收益处理方式
     * @return
     */
    public static BigDecimal calcExpectedRevenue(BigDecimal amount, BigDecimal rate, Integer periodLength, Integer periodUnit, Integer profitPlan) {
        BigDecimal result = BigDecimal.ZERO;
//        double monthRate = rate / 1200;
        if (profitPlan == null || "".equals(profitPlan)) {
            // 默认以一次性还款
            profitPlan = 4;
        }
        if (profitPlan == 3 || profitPlan == 4) {
            // 按期付息到期还本和一次性还款的计算方式
            Integer totalDay = null;
            switch (periodUnit) {
                case 1:
                    totalDay = 365;// 投资单位：天
                    break;
                case 2:
                    totalDay = 7;// 投资单位：周
                    break;
                case 3:
                    totalDay = 12;// 投资单位：月
                    break;
                case 4:
                    totalDay = 1;// 投资单位：年
                    break;
                default:
                    throw new ZhongyingiException(ResConstants.BORROW_PERIOD_UNIT_TYPE_NOT_INVAILD.getCode(), ResConstants.BORROW_PERIOD_UNIT_TYPE_NOT_INVAILD.getMsg());
            }

            if (periodUnit == 2) {
                result = amount.multiply(rate).multiply(BigDecimal.valueOf(periodLength)).multiply(BigDecimal.valueOf(totalDay))
                        .divide(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(365), 2, BigDecimal.ROUND_DOWN).setScale(2, BigDecimal.ROUND_DOWN);
            }else {
                result = amount.multiply(rate).multiply(BigDecimal.valueOf(periodLength))
                        .divide(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(totalDay), 2, BigDecimal.ROUND_DOWN).setScale(2, BigDecimal.ROUND_DOWN); //   /100*termTime /totalDay;
            }

            result = result.setScale(2, BigDecimal.ROUND_DOWN);

        } else if (profitPlan == 1 || profitPlan == 2) {
            // TODO 暂不考虑等额本息及等额本金的情况
//            BigDecimal monthIncome = amount.multiply(new BigDecimal(monthRate * Math.pow(1 +
//                    monthRate, periodLength))).divide(new BigDecimal(Math.pow(1 + monthRate, periodLength) - 1), 2,
//                    BigDecimal.ROUND_HALF_UP);
//            result = monthIncome.multiply(new BigDecimal(periodLength));
//            result = result.setScale(2, BigDecimal.ROUND_DOWN);
        }
        return result;

    }

    public static void main(String[] args) {
//        Map<String, Object> userStatusMap = new HashMap<String, Object>();
//        userStatusMap.put("userStatus","1");
//        //MapUtil mapUtil = new MapUtil(userStatusMap);
//        UserUtil.validatorUserInfo(userStatusMap);
        BigDecimal result = UserUtil.calcExpectedRevenue(new BigDecimal(2000),new BigDecimal(10),3,2,4);
        System.out.println(result);
    }

} 